package cn.edu.xmist.thread;

import java.util.concurrent.CountDownLatch;

/**
 * @author zhongjinbin (jinbin.zhong@nascent.cn)
 * @version 1.0.0
 * @date 2021/1/8 14:11
 * @description 线程之间通信
 */
public class CountdownLatchTest {

    public static void main(String[] args) {
        CountDownLatch count = new CountDownLatch(5);

        for (int i = 0; i < 5; i++) {
            new Thread(
                    new Runnable() {
                        @Override
                        public void run() {
                            System.out.println(Thread.currentThread().getName() + "线程执行中");
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            count.countDown();
                        }
                    })
                    .start();
        }

        System.out.println("主线程执行");
        // 等待子线程执行完成（阻塞）
        try {
            count.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("主线程完成");
    }
}
